//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2015 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

/// <summary>
/// Helper class containing generic functions used throughout the UI library.
/// </summary>

static public class NGUITools
{
	static public void SetDirty (UnityEngine.Object obj)
	{
		#if UNITY_EDITOR
		if (obj)
		{
			//if (obj is Component) Debug.Log(NGUITools.GetHierarchy((obj as Component).gameObject), obj);
			//else if (obj is GameObject) Debug.Log(NGUITools.GetHierarchy(obj as GameObject), obj);
			//else Debug.Log("Hmm... " + obj.GetType(), obj);
			UnityEditor.EditorUtility.SetDirty(obj);
		}
		#endif
	}

	/// <summary>
	/// Unity4 has changed GameObject.active to GameObject.activeself.
	/// </summary>
	[System.Diagnostics.DebuggerHidden]
	[System.Diagnostics.DebuggerStepThrough]
	static public bool GetActive (GameObject go)
	{
		return go && go.activeInHierarchy;
	}

	/// <summary>
	/// SetActiveRecursively enables children before parents. This is a problem when a widget gets re-enabled
	/// and it tries to find a panel on its parent.
	/// </summary>
	
	static public void SetActive (GameObject go, bool state) { SetActive(go, state, true); }
	
	/// <summary>
	/// SetActiveRecursively enables children before parents. This is a problem when a widget gets re-enabled
	/// and it tries to find a panel on its parent.
	/// </summary>
	
	static public void SetActive (GameObject go, bool state, bool compatibilityMode)
	{
		if (go)
		{
			if (state)
			{
				Activate(go.transform, compatibilityMode);
				#if UNITY_EDITOR
				if (Application.isPlaying)
					#endif
					CallCreatePanel(go.transform);
			}
			else Deactivate(go.transform);
		}
	}

	[System.Diagnostics.DebuggerHidden]
	[System.Diagnostics.DebuggerStepThrough]
	static public void SetActiveSelf (GameObject go, bool state)
	{
		go.SetActive(state);
	}

	/// <summary>
	/// Ensure that all widgets have had their panels created, forcing the update right away rather than on the following frame.
	/// </summary>
	
	[System.Diagnostics.DebuggerHidden]
	[System.Diagnostics.DebuggerStepThrough]
	static void CallCreatePanel (Transform t)
	{
//		UIWidget w = t.GetComponent<UIWidget>();
//		if (w != null) w.CreatePanel();
//		for (int i = 0, imax = t.childCount; i < imax; ++i)
//			CallCreatePanel(t.GetChild(i));
	}


	/// <summary>
	/// Activate the specified object and all of its children.
	/// </summary>
	
	static void Activate (Transform t) { Activate(t, false); }
	
	/// <summary>
	/// Activate the specified object and all of its children.
	/// </summary>
	
	static void Activate (Transform t, bool compatibilityMode)
	{
		SetActiveSelf(t.gameObject, true);
		
		if (compatibilityMode)
		{
			// If there is even a single enabled child, then we're using a Unity 4.0-based nested active state scheme.
			for (int i = 0, imax = t.childCount; i < imax; ++i)
			{
				Transform child = t.GetChild(i);
				if (child.gameObject.activeSelf) return;
			}
			
			// If this point is reached, then all the children are disabled, so we must be using a Unity 3.5-based active state scheme.
			for (int i = 0, imax = t.childCount; i < imax; ++i)
			{
				Transform child = t.GetChild(i);
				Activate(child, true);
			}
		}
	}
	
	/// <summary>
	/// Deactivate the specified object and all of its children.
	/// </summary>
	
	static void Deactivate (Transform t) { SetActiveSelf(t.gameObject, false); }

	static public string GetHierarchy (GameObject obj)
	{
		if (obj == null) return "";
		string path = obj.name;
		
		while (obj.transform.parent != null)
		{
			obj = obj.transform.parent.gameObject;
			path = obj.name + "\\" + path;
		}
		return path;
	}
}
